<?php

namespace gzapi\common\business;
use gzapi\common\model\GzHiseServiceProAcceptModel;
use gzapi\common\model\RlCompanyModel;
use gzapi\common\model\RlZsmsUserQueryModel;
use gzapi\common\model\GzHiseServiceProjectModel;

use gzapi\common\model\GzHiseServiceProTestModel;
use gzapi\common\model\GzHiseServiceInspectionModel;
use gzapi\common\model\GzHiseServiceProPaymentModel;
use gzapi\common\model\GzHiseServiceProTrainModel;
class Accept
{
    protected $GzHiseServiceProAccept;
    protected $GzHiseServiceProject;
    protected $RlCompany;
    protected $RlZsmsUserQuery;

    protected $GzHiseServiceProTest;
    protected $GzHiseServiceInspection;
    protected $GzHiseServiceProPayment;
    protected $GzHiseServiceProTrain;
    public function __construct()
    {
        $this->GzHiseServiceProAccept = new GzHiseServiceProAcceptModel;
        $this->RlCompany = new RlCompanyModel;
        $this->RlZsmsUserQuery = new RlZsmsUserQueryModel;
        $this->GzHiseServiceProject= new GzHiseServiceProjectModel;

        $this->GzHiseServiceProTest = new GzHiseServiceProTestModel;
        $this->GzHiseServiceInspection = new GzHiseServiceInspectionModel;
        $this->GzHiseServiceProPayment = new GzHiseServiceProPaymentModel;
        $this->GzHiseServiceProTrain = new GzHiseServiceProTrainModel;
    }
    //保存竣工
    public function acceptSave($where,$data)
    {
        $result = ['code'=>0,'msg'=>'保存失败'];
        //查找竣工信息是否存在
        $accept = $this->GzHiseServiceProAccept->getAccept($where);
        $info = $this->GzHiseServiceProject->where('azd_id',$data['azd_id'])->find();
        if($accept)
        {
            //判断巡检，培训，调试是否完成
         if($info['vip_type']==0 and $info['chain_type']==0){ 
           $yanzheng = $this->GzHiseServiceProTest->where('azd_id',$data['azd_id'])->where('ts_zt','<>',2)->value('azd_id');
            if($yanzheng)
            {
                $result = ['code'=>0,'msg'=>'请先完成调试'];
                return $result;
            }
            $yanzheng = $this->GzHiseServiceInspection->where('xj_azdbh',$data['azd_id'])->where('xj_shzt',0)->value('xj_azdbh');
            if($yanzheng)
            {
                $result = ['code'=>0,'msg'=>'请先完成巡检'];
                return $result;
            }
           }
            $yanzheng = $this->GzHiseServiceProPayment->where('azd_id',$data['azd_id'])->where('training_status','<>',3)->value('azd_id');
            if($yanzheng)
            {
                $result = ['code'=>0,'msg'=>'请先完成交付培训'];
                return $result;
            }
            $this->GzHiseServiceProTrain->where('azd_id',$data['azd_id'])->where('training_status','<>',3)->value('azd_id');
            if($yanzheng)
            {
                $result = ['code'=>0,'msg'=>'请先完成开工培训'];
                return $result;
            }
            $result['code'] = $this->GzHiseServiceProAccept->acceptUpate($where,$data);
            //修改项目主表培训状态
            if($result['code'])
            {
                //判断保修时间的依据，竣工还是发货
                //获取发货时间，如果大于12个月，按照发货时间+12个月，如果不到12个月，按照竣工时间
                $azd = $this->GzHiseServiceProject->where('azd_id',$data['azd_id'])->find();
                $begin_time =  date("Y-m-d",mktime(0, 0 , 0,date("m")-12,date("d"),date("Y")));
                if($azd['first_delivery_time'] && $azd['first_delivery_time'] < $begin_time )
                {
                    //发货
                    $bs = 2;
                    $fhtime = $azd['first_delivery_time'];
                    $baoxiu_time =  strtotime("$fhtime + 12 month");
                }else{
                    //竣工
                    $bs = 1;
                    $baoxiu_time = time();
                }
                $this->GzHiseServiceProject->where('azd_id',$data['azd_id'])->update(['gczt'=>5,'bx_kssj'=>$baoxiu_time,'bxq_upd_bs'=>$bs]);
                $result = ['code'=>1,'msg'=>'保存成功'];
            }
        }else{
            $result['code'] = $this->GzHiseServiceProAccept->acceptAdd($data);
            $result['msg'] = '保存成功';
        }
        return $result;
    }

    //获取竣工信息
    public function getAccept($where)
    {

        $accept = $this->GzHiseServiceProAccept->getAccept($where);
        if($accept)
        {
            $accept['acceptance_report'] = $accept['acceptance_report']?[$accept['acceptance_report']]:[];
            //验收单位
            $accept['acceptance_unit'] = $this->RlCompany->where('orgdept',$accept['acceptance_unit'])->value('orgname');
            //验收人员
            $accept['acceptance_personnel'] = $this->RlZsmsUserQuery->where('mobilphone',$accept['acceptance_personnel'])->value('user_name');
            //服务工程师
            $accept['service_engineer'] = $this->RlZsmsUserQuery->where('mobilphone',$accept['service_engineer'])->value('user_name');
            //创建人
            $accept['created_by'] = $this->RlZsmsUserQuery->where('mobilphone',$accept['created_by'])->value('user_name');
            //修改人
            $accept['last_upd_by'] = $this->RlZsmsUserQuery->where('mobilphone',$accept['last_upd_by'])->value('user_name');
            $accept['delivery_time'] = $accept['delivery_time']?$accept['delivery_time']:'';
        }

        return $accept;
    }
}
